package com.jinchengll.xigua.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.dueeeke.videocontroller.component.PrepareView;
import com.jinchengll.xigua.R;
import com.jinchengll.xigua.entity.Video;
import com.jinchengll.xigua.ui.listener.OnItemChildClickListener;
import com.jinchengll.xigua.ui.listener.OnItemClickListener;

import java.util.List;

/**
 * @author jinchengll
 * @see com.jinchengll.xigua.ui.fragment.RecommendFragment
 * @since 2021/10/6
 */
public class RecommendRecyclerViewAdapter
        extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Video> videoList;
    private final Context context;

    private OnItemChildClickListener mOnItemChildClickListener;

    private OnItemClickListener mOnItemClickListener;

    public RecommendRecyclerViewAdapter(List<Video> videoList, Context context) {
        this.videoList = videoList;
        this.context = context;
    }

    public void setVideoList(List<Video> videoList) {
        this.videoList = videoList;
    }

    public class RecommendViewHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener {
        public int mPosition;

        ImageView authorIcon;
        TextView authorName;
        TextView videoTitle;
        ImageView collectionImage;
        TextView collectionCount;
        TextView commentCount;
        ImageView dzIcon;
        TextView dzCount;
        // 视频播放器缩略图
        public ImageView mThumb;
        public PrepareView mPrepareView;
        public FrameLayout mPlayerContainer;

        public RecommendViewHolder(View view) {
            super(view);
            authorIcon = view.findViewById(R.id.authorIcon);
            authorName = view.findViewById(R.id.authorName);
            videoTitle = view.findViewById(R.id.videoTitle);
            collectionImage = view.findViewById(R.id.collectionImage);
            collectionCount = view.findViewById(R.id.collectionCount);
            commentCount = view.findViewById(R.id.commentCount);
            dzIcon = view.findViewById(R.id.dzIcon);
            dzCount = view.findViewById(R.id.dzCount);
            // 视频播放器相关
            mPlayerContainer = itemView.findViewById(R.id.playerContainer);
            mPrepareView = itemView.findViewById(R.id.prepareView);
            mThumb = mPrepareView.findViewById(R.id.thumb);
            if (mOnItemChildClickListener != null) {
                mPlayerContainer.setOnClickListener(this);
            }
            if (mOnItemClickListener != null) {
                itemView.setOnClickListener(this);
            }
            //通过tag将ViewHolder和itemView绑定
            itemView.setTag(this);
        }

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.playerContainer) {
                if (mOnItemChildClickListener != null) {
                    mOnItemChildClickListener.onItemChildClick(mPosition);
                }
            } else {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(mPosition);
                }
            }

        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(this.context).inflate(R.layout.video_item_recommend, parent, false);
        return new RecommendViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        RecommendViewHolder viewHolder = (RecommendViewHolder) holder;
        Video video = videoList.get(position);
        Glide.with(context).load(video.getHeadurl()).into(viewHolder.authorIcon);
        viewHolder.authorName.setText(video.getAuthor());
        viewHolder.videoTitle.setText(video.getVtitle());
        viewHolder.collectionCount.setText(String.valueOf(video.getCollectNum()));
        viewHolder.commentCount.setText(String.valueOf(video.getCommentNum()));
        viewHolder.dzCount.setText(String.valueOf(video.getLikeNum()));
        Glide.with(context)
                .load(video.getHeadurl())
                .transform(new CircleCrop())
                .into(viewHolder.authorIcon);
        Glide.with(context)
                .load(video.getCoverUrl())
                .into(viewHolder.mThumb);
        viewHolder.mPosition = position;
        // todo 收藏点赞图片需要根据服务器返回用户是否已经点赞来变换
    }

    @Override
    public int getItemCount() {
        return videoList.size();
    }


    public void setOnItemChildClickListener(OnItemChildClickListener onItemChildClickListener) {
        mOnItemChildClickListener = onItemChildClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }
}
